from rest_framework import status
from rest_framework.generics import CreateAPIView, RetrieveAPIView, GenericAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializer import CreateUserSerializer, UserInfoSerializer
from users.models import User
from django.db.models import Q
from rest_framework.permissions import IsAuthenticated


class UserView(CreateAPIView):
    """用户注册"""
    serializer_class = CreateUserSerializer


class UserCountView(APIView):
    def get(self, request, mobile, email):
        count = User.objects.filter(Q(mobile=mobile) | Q(email=email)).count()
        if count > 0:
            return Response({'msg': False})
        return Response({'msg': True})


class UserInfo(RetrieveAPIView):
    serializer_class = UserInfoSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        return self.request.user


class UserInfoDetail(GenericAPIView):
    queryset = User.objects.all()
    serializer_class = UserInfoSerializer

    def get(self, request, pk):
        user = self.get_object()
        serializer = self.get_serializer(user)
        return Response({'code': status.HTTP_200_OK, 'data': serializer.data})
